import React from 'react';
import { Form, Input, Button, Space, Select, DatePicker, Tooltip } from 'antd';
import { ReloadOutlined, SearchOutlined } from '@ant-design/icons';
import {
  btnPermission,
  downloadFile,
  formatDateTimeStamp,
} from '@/utils/utils';
import api from '@/services/api';
import { apiWeightExport } from '@/services/otherServices';
import { admin_prefix, admin_url } from '../../../config/admin.config';
import { history } from '@@/core/history';
import moment from 'moment';

const Search = (props: any) => {
  const [form] = Form.useForm();
  const { callbackValue, loading, pageSize, searchValue } = props;
  const onReset = () => {
    form.resetFields();
    callbackValue({});
  };
  const onFinish = (values: any) => {
    if (values.time) {
      values['startTime'] = formatDateTimeStamp(values.time[0], 'start');
      values['endTime'] = formatDateTimeStamp(values.time[1], 'end');
    }
    delete values.time;
    callbackValue(values);
  };
  return (
    <Form
      style={{ display: 'flex', flexWrap: 'wrap' }}
      form={form}
      name="control-hooks"
      onFinish={onFinish}
    >
      {btnPermission(history.location.pathname, 'btn_prod_search') && (
        <>
          <Form.Item name="company" style={{ marginBottom: 10, width: 200 }}>
            <Input placeholder={'请输入公司名'} />
          </Form.Item>
          <span style={{ padding: 4 }} />
          <Form.Item
            name="productLine"
            style={{ marginBottom: 10, width: 200 }}
          >
            <Input placeholder={'请输入产线名'} />
          </Form.Item>
          <span style={{ padding: 4 }} />
          <Form.Item name="classNo" style={{ marginBottom: 10, width: 200 }}>
            <Input placeholder={'请输入班次'} />
          </Form.Item>
          <span style={{ padding: 4 }} />
          <Form.Item name="orderNo" style={{ marginBottom: 10, width: 200 }}>
            <Input placeholder={'请输入订单号'} />
          </Form.Item>
          <span style={{ padding: 4 }} />
          <Form.Item name="time" style={{ marginBottom: 10, width: 300 }}>
            <DatePicker.RangePicker
              style={{ width: 300 }}
              placeholder={['开始时间', '结束时间']}
            />
          </Form.Item>
          <span style={{ padding: 4 }} />
          <Form.Item style={{ margin: 0, width: 80 }}>
            <>
              <Tooltip title="搜索">
                <Button
                  disabled={loading}
                  type="primary"
                  htmlType="submit"
                  icon={<SearchOutlined />}
                />
              </Tooltip>
              <span style={{ padding: 4 }} />
              <Tooltip title="重置">
                <Button
                  htmlType="button"
                  onClick={onReset}
                  icon={<ReloadOutlined />}
                />
              </Tooltip>
            </>
          </Form.Item>
        </>
      )}
      {btnPermission(history.location.pathname, 'btn_prod_export') && (
        <Form.Item style={{ margin: 0, width: 80 }}>
          <>
            <Button
              onClick={() => {
                const values = form.getFieldsValue();
                if (values.time) {
                  console.log('aa');
                  values['startTime'] = formatDateTimeStamp(
                    values.time[0],
                    'start',
                  );
                  values['endTime'] = formatDateTimeStamp(
                    values.time[1],
                    'end',
                  );
                } else {
                  values['startTime'] = new Date(
                    new Date(new Date().toLocaleDateString()).getTime() -
                      30 * 24 * 60 * 60 * 1000,
                  ).getTime();
                  values['endTime'] = new Date(
                    new Date(new Date().toLocaleDateString()).getTime() +
                      24 * 60 * 60 * 1000 -
                      1,
                  ).getTime();
                }
                delete values.time;
                apiWeightExport({
                  ...values,
                }).then((res: any) => {
                  downloadFile(res);
                });
              }}
              type={'primary'}
            >
              导出
            </Button>
          </>
        </Form.Item>
      )}
    </Form>
  );
};

export default Search;
